using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StructGoTest.GameObjects;

namespace StructGoTest {

	public class BlobTestGame : Game {

		//=====( Begin Globals )=====
		public static GraphicsDeviceManager Graphics;
		public static SpriteBatch SpriteBatch;
		public static Texture2D Blank; // This is the only Texture owned by the Game Engine.
		public static Texture2D WhiteHead;
		public static Texture2D GreenHead;
		public static Texture2D BigRedAlienHead;
		public static Texture2D BusinessGuyBody;
		public static Texture2D PlayerTexture;
		public static Player Player;
		public static IList<GameObject> GameObjects = new List<GameObject>();
		public static int Floor = 720 - 64; // 10% offset from the bottom.
		public static KeyboardState PreviousKeyboardState;
		public static KeyboardState CurrentKeyboardState;
		// Cowboy hat
		public static Texture2D CowboyHatTexture;
		public static HeadGear CowboyHat;
		//=====( End Globals )=======

		public static Point LookAtPoint = new Point(1000, 400);

		public BlobTestGame() {
			Graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
			Graphics.PreferredBackBufferWidth = 1280;
			Graphics.PreferredBackBufferHeight = 720;
		}


		protected override void LoadContent() {
			SpriteBatch = new SpriteBatch(GraphicsDevice);

			// Load assets.
			Blank = Content.Load<Texture2D>("Blank");
			WhiteHead = Content.Load<Texture2D>("WhiteHead");
			GreenHead = Content.Load<Texture2D>("GreenHead");
			BigRedAlienHead = Content.Load<Texture2D>("BigRedAlienHead");
			BusinessGuyBody = Content.Load<Texture2D>("BusinessGuyBody");
			PlayerTexture = Content.Load<Texture2D>("Player");
			CowboyHatTexture = Content.Load<Texture2D>("CowboyHat");

			// Create GameObjects.
			Player = new Player();
			CowboyHat = new HeadGear("Cowboy Hat", CowboyHatTexture, .75f);

			// Developers.
			GameObjects.Add(new Developer(200));
			GameObjects.Add(new Developer(640) {
				HeadTexture = GreenHead
			});
			GameObjects.Add(new Developer(1080) {
				HeadTexture = BigRedAlienHead
			});
		}


		protected override void Update(GameTime gameTime) {
			CurrentKeyboardState = Keyboard.GetState();
			Player.Update(gameTime);
			for (int i = 0; i < GameObjects.Count; i++) {
				GameObjects[i].Update(gameTime);
			}
			PreviousKeyboardState = CurrentKeyboardState;

			LookAtPoint = new Point(Mouse.GetState().X, Mouse.GetState().Y);

			base.Update(gameTime);
		}


		protected override void Draw(GameTime gameTime) {
			GraphicsDevice.Clear(Color.CornflowerBlue);

			SpriteBatch.Begin();
			//Player.Draw(gameTime);
			for (int i = 0; i < GameObjects.Count; i++) {
				GameObjects[i].Draw(gameTime);
			}

			//TODO
			SpriteBatch.Draw(Blank, new Rectangle(LookAtPoint.X-5, LookAtPoint.Y-5, 10, 10), Color.BlueViolet);
			//TODO
			SpriteBatch.End();

			base.Draw(gameTime);
		}

		//===============================================================================

		/*
		 * Determines the angle of a straight line drawn between point one and two. 
		 * The number returned, which is a float in degrees, tells us how much we have to rotate a horizontal line clockwise for it to match the line between the two points.
		 * See more at: http://wikicode.wikidot.com/get-angle-of-line-between-two-points#sthash.QCKMbIQ0.dpuf
		 */

		public static float GetAngleOfLineBetweenTwoPoints(Point p1, Point p2, bool inDegrees) {
			if (inDegrees) {
				return (float)(Math.Atan2(p2.Y - p1.Y, p2.X - p1.X) * (180 / Math.PI));
			}
			return (float)(Math.Atan2(p2.Y - p1.Y, p2.X - p1.X));
		}

	}

}